package com.zx.controller;

import com.zx.common.ResponseCode;
import com.zx.common.commonMethod;
import com.zx.entity.Collect;
import com.zx.entity.Food;
import com.zx.entity.Message;
import com.zx.entity.User;
import com.zx.repository.CategoryRepository;
import com.zx.repository.CollectRepository;
import com.zx.repository.FoodRepository;
import com.zx.repository.LinkRepository;
import com.zx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private UserService userService;

    @Autowired
    private CategoryRepository repository;

    @Autowired
    private CollectRepository collectRepository;

    @Autowired
    private FoodRepository foodRepository;

    @Autowired
    private LinkRepository linkRepository;

    public void Common(Map<String, Object> map, HttpSession session) {
        commonMethod.common(map, session, userService, repository,linkRepository,collectRepository);
    }

    @GetMapping("/list")
    public ModelAndView list(HttpSession session,
                             Map<String,Object> map){
        String username = (String) session.getAttribute("loginUser");
        User user = userService.findUserInfoByUsername(username);
        List<Collect> collects = collectRepository.findAllByUserId(user.getUserId());
        List<Food> foods = foodRepository.findAll();
        Common(map,session);
        map.put("collects",collects);
        map.put("foods",foods);
        return new ModelAndView("collect/list");
    }

    @GetMapping("/delete/{collectId}")
    public ModelAndView delete(@PathVariable("collectId") Integer id){
        Collect collect = collectRepository.getOne(id);
        collect.setStatus(0);
        collectRepository.save(collect);
        return new ModelAndView(new RedirectView("/foodshare/main/index"));
    }
}
